package taskOnClasses;

import java.io.*;
import java.util.*;

public class Client extends Person {

    private int ID;
    private int AccessCode;
    private double Balance; //ATTENTION: 'double' should not be used for monetary calculations
    private double Percentage;//
    private boolean newPercentage;
    private boolean isFrozen;//you only see what your eyes want to see... :)

    private class NotEnoughMoney extends Exception {

        @Override
        public String toString() {
            return "Not enough money on the account";
        }
    }

    private class AccountIsFrozen extends Exception {

        @Override
        public String toString() {
            return "Account is frozen";
        }
    }

    public Client(String Name, String Surname, int Age, int DateOfBirth, String Gender,
            int  someACode, int someID , double initBalance, double initComm, boolean initState) {
        super(Name, Surname, Age, DateOfBirth, Gender);
        this.ID = someID;
        this.AccessCode = someACode;
        this.Balance = initBalance;
        this.Percentage = initComm;
        this.newPercentage = false;
        this.isFrozen = initState;
    }

    public int getAccessCode() {
        return AccessCode;
    }

    public void setAccessCode(int AccessCode) {
        this.AccessCode = AccessCode;
    }

    public double getBalance() {
        return Balance;
    }

    public void setBalance(double Balance) {
        this.Balance = Balance;
    }

    public double getPercentage() {
        return Percentage;
    }

    public void setPercentage(double Percentage) {
        this.Percentage = Percentage;
    }

    public void setIsFrozen(boolean isFrozen) {
        this.isFrozen = isFrozen;
    }

    public boolean isNewPercentage() {
        return newPercentage;
    }

 //   public void setNewPercentage(boolean newPercentage) {
 //       this.newPercentage = newPercentage;
  //  }

    public void setNewPercentage(double newPercent) {
        this.Percentage = newPercent;
        this.newPercentage = true;
    }

    public void topUpAccount(double someAmmount) throws AccountIsFrozen {
        if (!isFrozen) {
            this.Balance += someAmmount;
        } else {
            throw new AccountIsFrozen();
        }
    }

    public void withdraw(double someAmmount) throws NotEnoughMoney, AccountIsFrozen {
        if (!isFrozen) {
            double fullAmmount = someAmmount + (someAmmount * Percentage) / 100;
            if (this.Balance - fullAmmount >= 0.0) {
                this.Balance -= fullAmmount;
            } else {
                throw new NotEnoughMoney();
            }
        } else {
            throw new AccountIsFrozen();
        }
    }

    public int getID() {
        return this.ID;
    }

    public void freezeAccount() {
        this.isFrozen = true;
    }

    public void unfreezeAccount() {
        this.isFrozen = false;
    }

    public boolean isFrozen() {
        return this.isFrozen;
    }

    public void setNewID(int someNumber) {
        this.ID = someNumber;
    }



    public void generateReport() {
        try {
            Date today = new Date();
            String curSurn = super.getSurname();
            Gender curGend = super.getGender();
            String opening = new String();
            String percentCondit = new String("\n");

            File outFile = new File(curSurn + "_" + ID + ".txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

            if (curGend == Gender.FEMALE) {
                opening = "Dear Ms ";
            } else {
                opening = "Dear Mr ";
            }
            if (newPercentage) {
                percentCondit = "The commision percentage has changed to " + Percentage + ".\n\n";
            }
            String output = opening + curSurn + ",\n\n"
                    + "As of date : " + today + " on your account, with ID: " + ID + ",\n"
                    + "you have exactly: " + Balance + " $.\n"
                    + percentCondit
                    + "Best regards,\n"
                    + "The Candy National Bank.";

            writer.append(output);
            writer.close();
        } catch (FileNotFoundException e) {
            //TODO: Add some warning or something...
        } catch (IOException e) {
            //TODO: Add some warning or something...
        }
    }

    @Override
    public String toString() {
        return super.toString() + " " + ID
                + " " + AccessCode
                + " " + Balance
                + " " + Percentage
                + " " + isFrozen;
    }
}
